package tools.icq.spam;

import core.ProxyConnection;
import java.util.Random;
import java.util.Vector;
import core.Main;
import tools.AddFucLib;
//import ru.caffeineim.protocols.icq.ProxyConnection;

/**
 *
 * @author dcrm
 */
public class CoreSpam {
    public static Vector<UinPrime> UIN_PRIMES = new Vector<UinPrime>();
    public static Vector<UinChild> UINS = new Vector<UinChild>();
    public static Vector<ProxyConnection> PROXYZ = new Vector<ProxyConnection>();
    public static Vector<ProxyConnection> SERVER_LOGIN = new Vector<ProxyConnection>();
    private boolean IO7=true;
    private Thread thread;
    private final String THREAD_NAME ="SpamCore";

    /**
     *
     * @param num - count childs
     * @param prx - use proxy?
     */
    public CoreSpam(int num, boolean prx)
    {
        UINS=Main.UINS;
        PROXYZ=Main.PROXYZ;
        SERVER_LOGIN=Main.SERVER_LOGIN;
        Random r = new Random();
        
        num=(num>(UINS.size()-1))?UINS.size():num;
        if (num==0) {num=UINS.size()-1;}
        for (int i=0; i<num;i++)
        {
            //UIN_PRIMES.add(new UinPrime(UINS.get(i), SERVER_LOGIN.get(r.nextInt(SERVER_LOGIN.size()-1)), (prx)?(PROXYZ.get(r.nextInt(PROXYZ.size()-1))):null));
        }
        Main.UIN_PRIMES=UIN_PRIMES;
        
        //thread = new Thread(this);
        //thread.setName(THREAD_NAME);
        //thread.start();
    }

    public CoreSpam(boolean prx)
    {
        this(0, prx);
    }
    
    /**
     * Send One msg
     * @param str - Message
     * @param uin - Uin
     */
    public void send(String str, String uin)
    { UIN_PRIMES.get(new Random().nextInt(UIN_PRIMES.size()-1)).sendBasicMessage(str, uin); }


    /**
     * Flood!
     * @param uin - Uin
     * @param type - Type flood: 0-MSG;1-AUT(T/F);2-REQ;3-ADD;4-REM;
     * @param cnt - Count
     * @param man - Count Uins
     * @param time - Interval
     * @param time - Interval Start
     * @param req - AUT(T/F)
     * @param rnd - RandomUin?
     * @param str - Message
     */
    public void flood(String uin, String type, String cnt, String man, String time, String time0, String req, String rnd, String str)
    {
        flood(str, uin, Integer.parseInt(man), Long.parseLong(time), Long.parseLong(time0), Integer.parseInt(cnt), Integer.parseInt(type), Boolean.parseBoolean(req), Boolean.parseBoolean(rnd));
    }

    /**
     * Flood!
     * @param str - Message
     * @param uin - Uin
     * @param man - Count Uins
     * @param time - Interval
     * @param time - Interval Start
     * @param cnt - Count
     * @param type - Type flood: 0-MSG;1-AUT(T/F);2-REQ;3-ADD;4-REM;
     * @param req - AUT(T/F)
     * @param rnd - RandomUin?
     */
    @SuppressWarnings("empty-statement")
    public void flood(String str, String uin, int man, long time, long time0, int cnt, int type, boolean req, boolean rnd)
    {
        int who;
        try {
            man=(man>(UIN_PRIMES.size()-1))?UIN_PRIMES.size():man;
            for (int i=0;i<man;i++)
            {
                who=(rnd)?new Random().nextInt(UIN_PRIMES.size()-1):i;
                //UIN_PRIMES.get(new Random().nextInt(UIN_PRIMES.size()-1)).send(str, uin, cnt, time);
                switch (type)
                {
                    case 0: new TMessager(str, uin, cnt, time, who, TMessager.TYPE_MSG, false); break;
                    case 1: new TMessager(str, uin, cnt, time, who, TMessager.TYPE_AUTH, req); break;
                    case 2: new TMessager(str, uin, cnt, time, who, TMessager.TYPE_AREC, false); break;
                    case 3: new TMessager(str, uin, cnt, time, who, TMessager.TYPE_ADD, false); break;
                    case 4: new TMessager(str, uin, cnt, time, who, TMessager.TYPE_REM, false); break;
                }
                //UIN_PRIMES.get(i).send(str, uin, cnt, time);
                Thread.sleep(time0);
            }
        } catch (Exception ex) { ex.printStackTrace();}
    }

    public void spam()
    {}

    public void destroy()
    {
        try {
            for (int i=0;i<UIN_PRIMES.size()-1;i++)
            {
                UIN_PRIMES.get(i).disconnect(true);
                Thread.sleep(100);
            }
            //
        } catch (Exception ex) { ex.printStackTrace();}
    }

    public synchronized void reParseLists()
    {
        UINS=AddFucLib.fillUinList("uin.txt", ";");
        PROXYZ=AddFucLib.fillProxyList("proxy.txt", ";");
        SERVER_LOGIN=AddFucLib.fillProxyList("server.txt", ";");
        reLoadList();
    }

    private synchronized void reLoadList()
    {
        Main.UINS=UINS;
        Main.PROXYZ=PROXYZ;
        Main.SERVER_LOGIN=SERVER_LOGIN;
    }

    /*public void run() {
        
        while(IO7)
        {
            Thread.sleep(millis);
        }
    }*/
    

}
